package
{
	import flash.display.MovieClip;
	
	import flashx.textLayout.formats.TextAlign;
	
	public class Keypad
	{
		public static var  MAX_BUTTONS:int		=35;
		protected var m_buttons:Vector.<InterfButton> = new Vector.<InterfButton>();
		protected var m_countButtons:int;
		protected var m_bEnabled:Boolean;
		public function Keypad()
		{
			m_bEnabled = true;
			m_countButtons = 0;
		}
		
		public function GetButton(id:int):InterfButton
		{
			for (var i:int = 0; i < m_countButtons; i++)
			{
				if (m_buttons[i].id == id)
				{
					return m_buttons[i];
				}
				
			}
			
			return null;
		}
		public function AddButton(id:int,posX:int,posY:int, _clip:MovieClip, str:String = null):void
		{
			var button:InterfButton = new InterfButton();
			
			button.id = id;
			button.clip = _clip;
			button.state = InterfButton.BUTTON_STATE_NOTHIMG;
			button.SetText(str);
			CalcBoundsFromFrame(button,posX,posY);
			
			CGame.GetGame().m_touchInterfMgr.RegisterElement(button);
			m_buttons[m_countButtons++] = button;
		}

		public function RemoveButton(id:int):void
		{
			for (var i:int = 0; i < m_countButtons; i++)
			{
				if (m_buttons[i].id == id)
				{
					for (var j:int = i + 1; j < m_countButtons; j++)
					{
						m_buttons[j - 1] = m_buttons[j];
					}
					
					m_countButtons--;
					
					break;
				}
			}
		}
			
		public function CalcBoundsFromFrame(button:InterfButton, x:int, y:int):void
		{
			button.posL.x = x;			
			button.posL.y = y;	
			
			//button.clip.nextFrame();
			button.posR.x = x + button.clip.width;
			button.posR.y = y + button.clip.height;

		}
		
		public function IsEnabled():Boolean
		{
			return m_bEnabled;
		}
		
		public function Draw():void
		{
			
			if (!IsEnabled())
				return;
			
			Game.GetMyGraphics().SetTextAlign(TextAlign.CENTER);
			Game.GetMyGraphics().SetTextSize(16);
			for (var i:int = 0; i < m_countButtons; i++)
			{
				if (m_buttons[i].m_visible == true && m_buttons[i].IsEnable())
				{
					Game.GetMyGraphics().DrawMovieClip(m_buttons[i].clip,m_buttons[i].posL.x,m_buttons[i].posL.y);
					if(m_buttons[i].HasText())
						Game.GetMyGraphics().DrawString(m_buttons[i].GetText(),m_buttons[i].GetCenterX(),m_buttons[i].GetCenterY() - 16,0xffffff);
				}
			}
			
		}
		
		public function Update():void
		{
			if (!IsEnabled())
				return;
			for (var i:int = 0; i < m_countButtons; i++)
			{
				if (m_buttons[i].state == InterfButton.BUTTON_STATE_END)
				{
					
					//if (m_buttons[i].m_endStateFrames > 0)
					//{
						m_buttons[i].state = InterfButton.BUTTON_STATE_NOTHIMG;
					//}
					
					//m_buttons[i].m_endStateFrames++;
				}
			}
		}
		
		
		public function HasKeyBeenReleased(key:int):Boolean
		{
			if (!IsEnabled())
				return false;
			for (var i:int = 0; i < m_countButtons; i++)
			{
				if (m_buttons[i].state == InterfButton.BUTTON_STATE_END)
				{
					if ((m_buttons[i].id == key))
					{
						return true;
					}
				}
			}
			return false;
		}
		
		
	}
}